package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import JDBC.DATABASE;
import VO.GroundType;

public class GroundTypeDao {
	/*
	 * 查询所有球场类型信息
	 */
	public ArrayList checkAllGroundType(){	//查询所有球场类型信息
		ArrayList groundtypes = new ArrayList();
		Connection conn=DATABASE.getConnection();
		String sql="select * from GROUND_TYPE order by typeid";
		//sql语句按照编号顺序查询球场类型所有信息
		ResultSet rs = DATABASE.query(sql);
		try {
			while(rs.next()){
				GroundType groundtype = new GroundType();
				groundtype.setId(rs.getInt("TYPEID"));
				groundtype.setTypename(rs.getString("TYPENAME"));
				groundtype.setDemo(rs.getString("DEMO"));
				groundtypes.add(groundtype);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block	
		}catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			DATABASE.close(conn);
		}
		return groundtypes;
	}
	/*
	 * 保存球场类型信息
	 */
	public void saveGroundType(GroundType gtype){
		Connection conn=DATABASE.getConnection();
		String sql=" INSERT INTO GROUND_TYPE VALUES (?,?,?)";
		try{
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setInt(1,gtype.getId());
			ps.setString(2,gtype.getTypename());
			ps.setString(3,gtype.getDemo());
			ps.executeUpdate();
			ps.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DATABASE.close(conn);
		}		
	}
	/*
	 *根据球场类型编号删除信息
	 */
	public void deleteGroundType(String typeid){
		Connection conn=DATABASE.getConnection();
		String sql = " delete from GROUND_TYPE where TYPEID=?";
		//sql语句根据类型编号进行；类型信息删除
		try{
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1,typeid);
			ps.executeUpdate();
			ps.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DATABASE.close(conn);
		}

	}
	/*
	 * 查找所有的类型
	 */
	
	public ArrayList findGroundType(){	
		ArrayList groundtypes = new ArrayList();
		Connection conn=DATABASE.getConnection();
		String sql="select typeid from GROUND_TYPE";
		//查看球场类型编号
		ResultSet rs = DATABASE.query(sql);
		
		try {
			while(rs.next()){
				GroundType groundtype = new GroundType();
				groundtype.setId(rs.getInt("TYPEID"));
				groundtypes.add(groundtype);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
		}catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			DATABASE.close(conn);
		}
		return groundtypes;
	}
}
